package ar.uade.edu.fain.ad.server.dao.entity;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "FACTURA")
@NamedQueries({
	@NamedQuery(name = "factura.byCajaByDate", query = 
			"	SELECT f" +
			"	FROM FacturaEntity f" +
			"	WHERE f.fecha = :fecha" +
			"		AND	f.caja.id = :idCaja"),
	@NamedQuery(name = "factura.sumarTotalByMedioPago", query = 
			"	SELECT SUM(f.total)" +
			"	FROM FacturaEntity f" +
			"	WHERE f.fecha = :fecha" +
			"		AND f.caja.id = :idCaja" +
			"		AND f.medioPago.tipo = :tipoMedioPago")	
})
public class FacturaEntity {

	@Id
	@GeneratedValue
	private Long id;

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name = "idFactura")
	private List<ItemFacturaEntity> items;

	@OneToMany(mappedBy = "factura", cascade = CascadeType.ALL)
	private Set<PedidoEntity> pedidos;

	@ManyToOne
	@JoinColumn(name = "idMedioPago")
	private MedioPagoEntity medioPago;

	@Column
	@Temporal(TemporalType.DATE)
	private Date fecha;

	@ManyToOne
	@JoinColumn(name = "idCaja")
	private CajaEntity caja;

	@Column
	private Double total;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<ItemFacturaEntity> getItems() {
		return items;
	}

	public void setItems(List<ItemFacturaEntity> items) {
		this.items = items;
	}

	public Set<PedidoEntity> getPedidos() {
		return pedidos;
	}

	public void setPedidos(Set<PedidoEntity> pedidos) {
		this.pedidos = pedidos;
	}

	public MedioPagoEntity getMedioPago() {
		return medioPago;
	}

	public void setMedioPago(MedioPagoEntity medioPago) {
		this.medioPago = medioPago;
	}

	public Date getFecha() {
		return fecha;
	}

	public void setFecha(Date fecha) {
		this.fecha = fecha;
	}

	public CajaEntity getCaja() {
		return caja;
	}

	public void setCaja(CajaEntity caja) {
		this.caja = caja;
	}

	public Double getTotal() {
		return total;
	}

	public void setTotal(Double total) {
		this.total = total;
	}
}
